
# Data preparation ----

rm(list = ls())
gc()

## Set wd
setwd("")

## Set seed
set.seed(123)

## Run analysis/script for each ballot
source("rcs_climat.R")
source("rcs_ocde.R")
source("rcs_prop26.R")
source("rcs_prop27.R")



# Table 1 ----

df_list <- list(cross_climat, cross_OCDE, cross_prop26, cross_prop27)

df_list <- map(df_list, ~ .x %>%
                 mutate(outcome = case_when(
                   voteinlineparty_rcd == 0 & voteinlinearg_rcd == 0 ~ "Not inline and not consistent",
                   voteinlineparty_rcd == 1 & voteinlinearg_rcd == 0 ~ "Inline but not consistent",
                   voteinlineparty_rcd == 0 & voteinlinearg_rcd == 1 ~ "Not inline but consistent",
                   voteinlineparty_rcd == 1 & voteinlinearg_rcd == 1 ~ "Inline and consistent")) %>%
                 filter(!is.na(outcome)))

cross_climat <- df_list[[1]]
cross_OCDE <- df_list[[2]]
cross_prop26 <- df_list[[3]]
cross_prop27 <- df_list[[4]]

combined_df <- bind_rows(df_list)

combined_df$Freq <- round(combined_df$Freq, 1)
combined_df



# Merge plots ----

## Define function to merge
library(ggpubr)

plot_objects <- ls(pattern = "^p\\d+_")

prefixes <- unique(gsub("^p(\\d+)_.*", "\\1", plot_objects))

arranged_plots_list <- list()

for (prefix in prefixes) {
  prefix_plots <- plot_objects[grep(paste0("^p", prefix, "_"), plot_objects)]
  plots <- lapply(prefix_plots, get)
  arranged_plot <- ggarrange(plotlist = plots, common.legend = T, legend = "bottom")
  arranged_plots_list[[prefix]] <- arranged_plot
}

assign("arranged_plots_list", arranged_plots_list, envir = .GlobalEnv)


## Figure 2a ----
arranged_plots_list[["01"]]
ggsave(filename = "2a_partycueknow.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure 2b ----
arranged_plots_list[["02"]]
ggsave(filename = "2b_dkarg.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure 2c ---- 
arranged_plots_list[["04"]]
ggsave(filename = "2c_inline.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure  2d ----
arranged_plots_list[["03"]]
ggsave(filename = "2d_consistent.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C1.2a ----
ggarrange(pcertain01_climat, pcertain01_OCDE, pcertain01_prop26, pcertain01_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pcertain2a_partycueknow.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C1.2b ----
ggarrange(pcertain02_climat, pcertain02_OCDE, pcertain02_prop26, pcertain02_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pcertain2b_dkarg.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C1.2c ----
ggarrange(pcertain04_climat, pcertain04_OCDE, pcertain04_prop26, pcertain04_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pcertain2c_inline.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C1.2d ----
ggarrange(pcertain03_climat, pcertain03_OCDE, pcertain03_prop26, pcertain03_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pcertain2d_consistent.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C3a.2d ----
ggarrange(pzero_climat, pzero_OECD, pzero_prop26, pzero_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pzero2d.png", units = "cm", width = 12, height = 12, dpi = 600)

## Figure C3b.2d ----
ggarrange(pfive_climat, pfive_OECD, pfive_prop26, pfive_prop27, common.legend = T, legend = "bottom")
ggsave(filename = "pfive2d.png", units = "cm", width = 12, height = 12, dpi = 600)











